package com.mi.system.event;

import cn.hutool.core.bean.BeanUtil;
import com.mi.common.log.event.LogEvent;
import com.mi.system.entity.Log;
import com.mi.system.service.LogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 *  Log listener
 *
 *  @author     jin_huaquan
 *  @date      2021/11/3 13:33
 *  @version   1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LogListener {

    private final LogService logService;

    @Async
    @EventListener(LogEvent.class)
    public void saveLog(LogEvent logEvent) {
        LogEvent.Log source = (LogEvent.Log) logEvent.getSource();

        Log log = new Log();

        BeanUtil.copyProperties(source, log);

        logService.save(log);
    }
}
